package com.zimin.cloud.system.biz.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zimin.cloud.mine.pojo.system.business.PostBO;
import com.zimin.cloud.mine.pojo.system.view.PostVO;
import com.zimin.cloud.system.biz.IPostBiz;
import com.zimin.cloud.system.pojo.entity.Post;
import com.zimin.cloud.system.pojo.entity.Role;
import com.zimin.cloud.system.service.IAccountPostService;
import com.zimin.cloud.system.service.impl.PostServiceImpl;
import com.zimin.common.annotation.Biz;
import com.zimin.common.enums.Status;
import com.zimin.common.utils.BeanUtils;
import com.zimin.shared.pojo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 * 岗位信息 业务对象
 * </p>
 *
 * @author zimin
 * @since 2021-12-30 11:05
 */
@Biz("postBiz")
public class PostBizImpl extends PostServiceImpl implements IPostBiz {

    @Autowired
    private IAccountPostService accountPostService;

    @Override
    public PageVO<PostVO> pagePostList(Page<Post> postPage, PostBO postBO) {
        var postPageList = page(postPage, super.queryWrappers(postBO));
        return pageToPageVO(postPageList, BeanUtils.listEntityToView(postPageList.getRecords(), PostVO.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(PostBO postBO) {
        var post = BeanUtils.coverClass(postBO, Post.class);
        return super.save(post);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(PostBO postBO) {
        var post = BeanUtils.coverClass(postBO, Post.class);
        return super.updateById(post);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(List<String> idList) {
        var removeFlag = super.removeByIds(idList);
        if (removeFlag) {
            accountPostService.removeByPostIds(idList);
        }
        return removeFlag;
    }

    @Override
    public List<PostVO> allPost() {
        var postList = lambdaQuery().eq(Post::getStatus, Status.ABLE.ordinal()).list();
        return BeanUtils.listEntityToView(postList, PostVO.class);
    }
}
